# -*- coding: utf-8 -*-
# @Author  : pony.guan
# @Time    : 2021/12/27 15:43
# Description:
import json
import requests

from checkContainerInformation import repeat_containers
from config import domain

# 缓存货架控制权切换url
switch_url = f'http://{domain}/HaiqEssAplController/lightOffCallback'

# 下架任务成功回传重推
callback_re_push_url = f'http://{domain}/HaiqEssAplController/adapterCallback'

# 容器离场
container_move_out_url = f'http://{domain}/container/moveOut'

headers = {
    "Content-Type": "application/json"
}


def switch(tagCode) -> str:
    data = {
        "tagCode": tagCode,
        "eventCode": "LIGHT_OFF",
    }
    r = requests.post(url=switch_url, data=json.dumps(data, ensure_ascii=False), headers=headers)
    if r.status_code != 200:
        return f'{r.status_code} {r.text}'
    return json.dumps(r.json(), indent=4)


def callback_re_push(taskGroupCode, taskCode, containerCode, locationCode) -> str:
    data = {
        "eventCode": "CALLBACK_OF_TOTE_LOADED_BY_ROBOT",
        "taskGroupCode": taskGroupCode,  # 业务任务组号
        "taskCode": taskCode,  # 任务号
        "taskStatus": "FINISHED",
        "taskTemplateCode": "TOTE_RACK_BUFFER_STATION_OUTBOUND",
        "actionCode": "load",
        "robotCode": "kubot-7",
        "containerCode": containerCode,  # 料箱编码
        "trayLevel": 64,
        "stationCode": "LA_SHELF_STORAGE",
        "locationCode": locationCode,  # 库位编码
        "updateTime": 1637560241622
    }

    r = requests.post(url=callback_re_push_url, data=json.dumps(data, ensure_ascii=False), headers=headers)
    if r.status_code != 200:
        return f'{r.status_code} {r.text}'
    return json.dumps(r.json(), indent=4)


def container_move_out(containerCode, positionCode) -> str:
    data = {
        "containerMoveOuts": [
            {
                "containerCode": containerCode,  # 料箱编码
                "positionCode": positionCode  # 位置编码
            }
        ]
    }
    r = requests.post(url=container_move_out_url, data=json.dumps(data, ensure_ascii=False), headers=headers)
    if r.status_code != 200:
        return f'{r.status_code} {r.text}'
    return json.dumps(r.json(), indent=4)


def query_repeat_containers():
    return repeat_containers()
